/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.log.annotation.Log;
import com.comac.ins.common.log.enums.BusinessType;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.common.web.core.BaseController;
import com.comac.ins.isfd.domain.bo.IsfdModelManagementTaskBo;
import com.comac.ins.isfd.domain.bo.query.IsfdModelManagementTaskQueryBo;
import com.comac.ins.isfd.domain.vo.*;
import com.comac.ins.isfd.service.IIsfdModelManagementTaskService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 模型管理-模型组建任务controller
 *
 * @author hxloongs
 * @date 2024-08-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/isfd/modelManagementTask")
public class IsfdModelManagementTaskController extends BaseController {

    @Autowired
    private IIsfdModelManagementTaskService isfdModelManagementTaskService;

    /**
     * 模型组建任务分页列表
     */
    @SaCheckPermission("isfd:modelManagementTask:list")
    @PostMapping("/list")
    public TableDataInfo<IsfdModelManagementTaskVo> list(@RequestBody IsfdModelManagementTaskQueryBo bo) {
        return isfdModelManagementTaskService.queryPageList(bo);
    }

    /**
     * 模型组建左侧树查询
     */
    @SaCheckPermission("isfd:modelManagementTask:tree")
    @GetMapping("/listTree")
    public R<List<IsfdModelManagementStructureDefinitionTreeNodeVo>> listTree(@RequestParam(value = "modelNumber") String modelNumber,
                                                                              @RequestParam(value = "batchNumber", required = false) String batchNumber) {
        return R.ok(isfdModelManagementTaskService.listTree(modelNumber, batchNumber));
    }

    /**
     * 模型组建全机模型下载
     */
    @SaCheckPermission("isfd:modelManagementTask:bom:download")
    @GetMapping("/bom/download")
    public void bomDownload(@NotEmpty @RequestParam(value = "modelNumber") String modelNumber,
                            @RequestParam(value = "batchNumber", required = false) String batchNumber,HttpServletResponse response) {
        isfdModelManagementTaskService.allBomDownload(modelNumber, batchNumber,response);
    }

    /**
     * 模型组建bom表导入
     */
    @SaCheckPermission("isfd:modelManagementTask:bom:import")
    @Log(title = "模型组建bom表导入", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/bom/import", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<IsfdModelManagementTaskImportVo> checkImport(@RequestParam("file") MultipartFile file) {
        IsfdModelManagementTaskImportVo taskImportVo = isfdModelManagementTaskService.bomImport(file);
        return R.ok(taskImportVo);
    }

    /**
     * 根据任务id查询关联的零件列表
     */
    @SaCheckPermission("isfd:modelManagementTask:getPartListByTaskId")
    @GetMapping("/getPartListByTaskId")
    public R<List<IsfdModelManagementPartVo>> list(@RequestParam("id") Long id) {
        return R.ok(isfdModelManagementTaskService.getPartListByTaskId(id));
    }

    /**
     * 获取模型组建任务详细信息，包含：任务、零件列表 和 计算文件操作记录
     */
    @SaCheckPermission("isfd:modelManagementTask:queryById")
    @GetMapping("/queryById")
    public R<IsfdModelManagementTaskDetailVo> queryById(@NotNull(message = "主键不能为空")
                                                        @RequestParam("id") Long id) {
        return R.ok(isfdModelManagementTaskService.queryDetailVoById(id));
    }

    /**
     * 删除模型组建任务
     */
    @SaCheckPermission("isfd:modelManagementTask:remove")
    @Log(title = "模型组建任务", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    public R<Void> remove(@NotNull(message = "主键不能为空") @RequestBody List<Long> ids) {
        return toAjax(isfdModelManagementTaskService.deleteWithValidByIds(ids));
    }

    /**
     * 模型组建任务保存，包括新增（普通新增和复制新增）、修改
     */
    @SaCheckPermission("isfd:modelManagementTask:save")
    @Log(title = "模型组建任务保存", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public R<IsfdModelManagementTaskSaveVo> save(@RequestBody IsfdModelManagementTaskBo isfdModelManagementTaskBo) {
        return R.ok(isfdModelManagementTaskService.save(isfdModelManagementTaskBo));
    }

    /**
     * 下载BDF文件
     */
    @SaCheckPermission("isfd:modelManagementTask:bdf:download")
    @Log(title = "下载BDF文件", businessType = BusinessType.EXPORT)
    @GetMapping("/bdf/download")
    public void bdfDownload(@NotNull(message = "主键不能为空") @RequestParam("id") Long id, HttpServletResponse response) {
        isfdModelManagementTaskService.bdfDownload(id, response);
    }


    /**
     * 导出BOM清单
     */
    @SaCheckPermission("isfd:modelManagementTask:bom:export")
    @Log(title = "导出BOM清单", businessType = BusinessType.EXPORT)
    @GetMapping("/bom/export")
    public void bomExport(@NotNull(message = "主键不能为空") @RequestParam("id") Long id, HttpServletResponse response) {
        isfdModelManagementTaskService.bomExport(id, response);
    }
}
